package io.renren.modules.nuaa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.nuaa.dao.TQuestionDao;
import io.renren.modules.nuaa.dao.TQuestionItemDao;
import io.renren.modules.nuaa.dto.TQuestionDTO;
import io.renren.modules.nuaa.dto.TQuestionItemDTO;
import io.renren.modules.nuaa.dto.TQuestionOptionsDTO;
import io.renren.modules.nuaa.entity.TQuestionEntity;
import io.renren.modules.nuaa.entity.TQuestionItemEntity;
import io.renren.modules.nuaa.service.TQuestionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 问卷
 *
 * @author whc 15953295779@126.com
 * @since 3.0 2023-11-03
 */
@Service
public class TQuestionServiceImpl extends CrudServiceImpl<TQuestionDao, TQuestionEntity, TQuestionDTO> implements TQuestionService {

    @Autowired
    private TQuestionItemDao tQuestionItemDao;

    @Override
    public QueryWrapper<TQuestionEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<TQuestionEntity> wrapper = new QueryWrapper<>();
        String title = (String) params.get("title");
        String categoryId = (String) params.get("categoryId");
        if(StringUtils.isNotBlank(title)){
            wrapper.like("title",title);
        }
        if(StringUtils.isNotBlank(categoryId)){
            wrapper.eq("category_id",Integer.parseInt(categoryId));
        }


        return wrapper;
    }

    /**
     * todo 待完善
     * @param question
     */
    @Override
    public void addOrUpdate(TQuestionDTO question) {
        TQuestionEntity q =null;
        if(q==null){
            q = this.baseDao.selectOne(new QueryWrapper<TQuestionEntity>().eq("class_room_id",question.getCategoryId())
            );
        }
        if(question.getId()==null ){
            q =new TQuestionEntity();
            q.setCategoryId(question.getCategoryId());
            q.setCreator("");
            q.setCreateDate(new Date());
            baseDao.insert(q);
        }else{
            q = baseDao.selectById(question.getId());
        }
        q.setUpdateDate(new Date());
        q.setStatus(question.getStatus());
        baseDao.updateById(q);
        List<TQuestionItemEntity> old = new ArrayList<>();
        if(question.getId()!=null){
            QueryWrapper qi = new QueryWrapper<>().eq("question_Id",question.getId());
            old = tQuestionItemDao.selectList(qi);
        }
        List<Integer> oldId = old.stream().map(v->v.getId().intValue()).collect(Collectors.toList());
        //保存题目
        for(TQuestionItemDTO questionItemDTO: question.getQuestionItemList()){
            TQuestionItemEntity entity = null;
            if(questionItemDTO.getId()!=null){
                entity = tQuestionItemDao.selectById(questionItemDTO.getId());
                oldId.remove(questionItemDTO.getId());
            }
            if(entity==null){
                entity = new TQuestionItemEntity();
                entity.setCreateDate(new Date());
            }else{
                entity.setUpdateDate(new Date());
            }
            entity.setQuestionId(q.getId().intValue());
            entity.setType(questionItemDTO.getType());
            entity.setSort(questionItemDTO.getSort());
            entity.setTitle(questionItemDTO.getTitle());
            if(questionItemDTO.getOptions()!=null&&questionItemDTO.getOptions().size()>0){
                entity.setContent(JSONObject.toJSONString(questionItemDTO.getOptions()));
            }
            if(entity.getId()==null){
                tQuestionItemDao.insert(entity);
            }else{
                tQuestionItemDao.updateById(entity);
            }

        }
        if(oldId.size()>0){
            for(Integer item : oldId){
                tQuestionItemDao.deleteById(item);

            }

        }
    }

    /**
     * todo 待完善
     * @param categoryId
     * @return
     */
    @Override
    public TQuestionDTO getQuestion(Integer categoryId) {

        TQuestionEntity questionEntity = this.baseDao.selectOne(new QueryWrapper<TQuestionEntity>().eq("category_id",categoryId)
        );
        TQuestionDTO question = new TQuestionDTO();
        question.setId(questionEntity.getId());
        question.setCategoryId(questionEntity.getCategoryId());
        question.setStatus(questionEntity.getStatus());
        if(questionEntity!=null){
            //获取题目列表
            List<TQuestionItemEntity> itemList = tQuestionItemDao.selectList(
                    new QueryWrapper<TQuestionItemEntity>()
                            .eq("question_id",questionEntity.getId().intValue()));

            List<TQuestionItemDTO> questionList = new ArrayList(itemList.size());
            for(TQuestionItemEntity item: itemList){
                TQuestionItemDTO itemDTO = new TQuestionItemDTO();
                Integer type = item.getType();
                BeanUtils.copyProperties(item,itemDTO);
                if(item.getContent()!=null){
                    try{
                        itemDTO.setOptions(JSONObject.parseArray(item.getContent(), TQuestionOptionsDTO.class));
                        if(itemDTO.getOptions()!=null){
                            //itemDTO.setAnswer("1");
                            for(TQuestionOptionsDTO optionsDTO: itemDTO.getOptions()){
                                optionsDTO.setValue(optionsDTO.getId().toString());
                            }
                        }

                    }catch (Exception e){

                    }
                }
                //单选题  多选题   评分题

                questionList.add(itemDTO);
            }
            question.setQuestionItemList(questionList);
        }
        return ConvertUtils.sourceToTarget(question,TQuestionDTO.class);

    }
}